﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;



namespace Performance_Test
{
    public partial class Performance_Test
    {
        public bool scaner_enable = true;
        public int DE_Count = 0;
        private void button1_Click(object sender, EventArgs e)
        {

            listExpectedData.Items.Clear();
            expected_data.Clear();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                   System.IO.StreamReader(openFileDialog1.FileName);

                while (sr.Peek() >= 0)
                {

                    expected_data.Add(sr.ReadLine());
                    expected_data.ToArray();
                }
                for (int i = 0; i < expected_data.Count; i++)
                {
                    listExpectedData.Items.Add(expected_data[i]);

                }

                sr.Close();

            }
        }
        private void btnEnableDisable_Click(object sender, EventArgs e)
        {
            //int len;
            //string last_char;

            //do
            //{
            //    ComPort.Write("D");
            //            txtReceivedData.Text += "D";
            //            System.Threading.Thread.Sleep

            //         ComPort.Write("E");

            //             txtReceivedData.Text += "E";


            //         InputData = ComPort.ReadExisting();
            //         len = InputData.Length;
            //         if (len != 0)
            //         {
            //             last_char = InputData.Substring(len - 1, 1);

            //             txtReceivedData.Invoke(new EventHandler(delegate
            //             {

            //                 txtReceivedData.Text += InputData;
            //                 if (last_char == "\r")
            //                 {
            //                     txtReceivedData.Text += "\n";
            //                     Execuse.TextReceivedData = txtReceivedData.Text;
            //                 }
            //             }));
            //         }


            //    }
            //} while (true);

            if (btnEnableDisable.Text.Equals("Enable&&Disable HH"))
            {
                btnEnableDisable.Text = "Stop Enable&&Disable HH";
              //  m_stop = true;
                //StartThread();
                timer1.Stop();
                timer1.Interval = Convert.ToInt32(txtMiSeconds.Text);
                timer1.Start();

            }
            else
            {
                btnEnableDisable.Text = "Enable&&Disable HH";
                timer1.Stop();
               // scaner_enable = true;
                // ComPort.Write("E");
                //if (backgroundWorker1.WorkerSupportsCancellation == true)
                //{
                //    // Cancel the asynchronous operation.
                //    backgroundWorker1.CancelAsync();
                //}
            }

        }

        private void scanning_display_missread()
        {
            lblMiScanningTime.Invoke(new EventHandler(delegate
            {

                lblMiScanningTime.Text = st.ToString();

            }));
            
            lblMiGoodRead.Invoke(new EventHandler(delegate
            {

                GoodCount = Execuse.Good_Count_File(GoodCount, InputData, expected_data);
                lblMiGoodRead.Text = GoodCount.ToString();
            }));
            lblMiIncorrect.Invoke(new EventHandler(delegate
            {

                IncorrectCount = Execuse.Incorrect_Count_File(IncorrectCount, InputData, expected_data);
                lblMiIncorrect.Text = IncorrectCount.ToString();
            }));
            lblMiNoRead.Invoke(new EventHandler(delegate
            {

              lblMiNoRead.Text = MissRead.ToString();
            }));
            lblMiSumLabels.Invoke(new EventHandler(delegate
            {

                lblMiSumLabels.Text = Execuse.SumLabels_Missread(GoodCount, IncorrectCount, MissRead).ToString();
            }));
            lblMiReadingRate.Invoke(new EventHandler(delegate
            {

                lblMiReadingRate.Text = Execuse.ReadingRate(Execuse.SumLabels_Missread(GoodCount, IncorrectCount, MissRead), GoodCount).ToString() + "%";
            }));
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            
            if (scaner_enable == true)
            {
                ComPort.Write("D");
                scaner_enable = false;
                timer1.Interval = Convert.ToInt32(txtMiSeconds.Text);
                txtReceivedData.Text += "D";
                // InputData += "D";
            }
            else
            {
                ComPort.Write("E");
                scaner_enable = true;
                timer1.Interval = Convert.ToInt32(txtMiSeconds.Text);
                txtReceivedData.Text += "E";
                // InputData += "E";
                DE_Count++;
            }
            timer1.Start();
        }
    }
}